<?
/*
 * selectArray
 * @auth Anselmo Battisti
 * @mail anselmobattisti@gmail.com
 *
 * @version 1.0
 *
 * @example
 require_once "selectArray.php";
 $fruit[0] = 'Lemon';
 $fruit[1] = 'Banana';
 $fruit[2] = 'Orange';
 echo Sistema_Componente_Select::getHtmlSelectArray($fuit,'fruit',$_POST['fuit']);
 */
class Sistema_Componente_Select
{
	/**
	 * getHtmlSelectArray
	 *
	 * @abstract Gera um SELECT html em fun��o de um vetor, a chave do vetor � o value e o option
	 */
	static function getHtmlSelectArray($array, $name, $selected, $null = false)
	{
		if(!is_array($array) && !$null){
			$html = "Erro ao gerar o select";
		} else {
			$html = '<select name="'.$name.'" id="'.$name.'" size="1">';

			if($null) {
				$html .= "<option value=''>-- ".Sistema_I386::getString('msg_root')." --</value>";
			}

			if(is_array($array)){
				foreach($array as $k=>$v){
					unset($s);
					if($k == $selected) $s = "selected = 'true'";
	
					$html .= '<option value="'.$k.'" '.$s.'>'.$v.'</option>';
				}
			}
			$html .= '</select>';
		}
		return $html;
	}
	
	/**
	 * getHtmlSelectSql
	 *
	 * @abstract Gera um SELECT html em fun��o de uma sql
	 */
	static function getHtmlSelectSql($sql, $name, $selected, $null = false)
	{
		
		$array = Sistema_Conecta::select($sql);

		foreach($array as $v){
			$a[$v[0]] = $v[1]; 
		}
		
		return self::getHtmlSelectArray($a,$name,$selected,$null);
	}
}
?>